package cn.study.project.stackview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.StackView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private StackView mStackView;
    private int[] myIDs={R.drawable.abc_0,R.drawable.abc_1,R.drawable.abc_2,R.drawable.abc_3};
    private List<Integer> myImages=new ArrayList<>();
    private Timer mTimerDown;
    private Timer mTimerUp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mStackView=findViewById(R.id.stackView);
        for (int myID : myIDs) myImages.add(myID);
        ImageAdapter adapter=new ImageAdapter(myImages,this);
        mStackView.setAdapter(adapter);
        mStackView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),"eg:"+position,Toast.LENGTH_LONG).show();
            }
        });

        findViewById(R.id.down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTimerDown!=null) mTimerDown.cancel();
                if(mTimerUp!=null) mTimerUp.cancel();
                mTimerDown=new Timer();
                mTimerDown.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mStackView.showNext();
                            }
                        });
                    }
                },0,1000);
            }
        });

        findViewById(R.id.up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTimerDown!=null) mTimerDown.cancel();
                if(mTimerUp!=null) mTimerUp.cancel();
                mTimerUp=new Timer();
                mTimerUp.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mStackView.showPrevious();
                            }
                        });
                    }
                },0,1000);
            }
        });
    }
}